package com.lanou3g.ptp.api.web.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.lanou3g.ptp.api.resolver.annotation.SsoStudent;
import com.lanou3g.ptp.api.resolver.annotation.SsoTeacher;
import com.lanou3g.ptp.entity.Approval;
import com.lanou3g.ptp.entity.Student;
import com.lanou3g.ptp.entity.Teacher;
import com.lanou3g.ptp.service.ApprovalService;
import com.lanou3g.ptp.utils.PtpResult;
import com.lanou3g.ptp.utils.PtpResultUtils;
import com.lanou3g.ptp.vo.Pageable;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;


@Api(description = "请假审核")
@RestController
@Slf4j
public class ApprovalController {

    @Reference
    private ApprovalService approvalService;

    @ApiOperation("学生申请请假")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "用户信息key值", paramType = "header")
    })
    @PostMapping("/approval")
    public PtpResult postApproval(@ApiIgnore @SsoStudent Student student,
                                  @RequestBody Approval approval) {
        approval.setStudentId(student.getId());
        boolean result = approvalService.postApproval(approval);
        return result ? PtpResultUtils.ok() : PtpResultUtils.error(440, "提交请假失败");
    }


    @ApiOperation("学生查询请假信息接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "用户信息key值", paramType = "header")
    })
    @GetMapping("/approvals")
    public PtpResult listMyApprovals(@ApiIgnore @SsoStudent Student student) {
        List<Approval> approvals = approvalService.findByStudentId(student.getId());
        return PtpResultUtils.ok(approvals);
    }

    /**
     * @param page
     * @return
     * @author 武奇
     */
    @ApiOperation("查询所有未审核的请假信息接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "用户信息key值", paramType = "header")
    })
    @GetMapping("/approvals/unpass")
    public PtpResult unPassApprovals(@ApiIgnore @SsoTeacher Teacher teacher,
                                      Pageable page) {
        PageInfo<Approval> pageInfo = approvalService.unPassApprovals(teacher.getId(), page);
        return PtpResultUtils.ok(pageInfo);
    }

    @ApiOperation("Post方式查询所有未审核的请假信息接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "用户信息key值", paramType = "header")
    })
    @PostMapping("/approvals/unpass")
    public PtpResult unPassApprovalsPost(@ApiIgnore @SsoTeacher Teacher teacher,
                                          @RequestBody Pageable page) {
        return unPassApprovals(teacher, page);
    }


    /**
     * @param teacher
     * @param approvalId
     * @return
     * @author 武奇
     */
    @ApiOperation("老师审核-通过请假审核")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "用户信息key值", paramType = "header"),
            @ApiImplicitParam(name = "approvalId", value = "假条id"),
    })
    @PostMapping("/approval/pass/{approvalId}")
    public PtpResult passApproval(@ApiIgnore @SsoTeacher Teacher teacher,
                                   @PathVariable Long approvalId) {
        Boolean success = approvalService.passApproval(teacher.getUser().getName(), approvalId);
        try{
            return success ? PtpResultUtils.ok() : PtpResultUtils.error(420, "操作失败");
        }catch (Exception e){
            e.printStackTrace();
            return PtpResultUtils.error(500);
        }
    }

    /**
     * @param teacher
     * @param approvalId
     * @return
     * @author 武奇
     */
    @ApiOperation("老师审核-拒绝请假审核")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "用户信息key值", paramType = "header"),
            @ApiImplicitParam(name = "approvalId", value = "假条id"),
    })
    @PostMapping("/approval/denied/{approvalId}")
    public PtpResult denyApproval(@ApiIgnore @SsoTeacher Teacher teacher,
                                   @PathVariable Long approvalId) {
        Boolean success = approvalService.denyApproval(teacher.getUser().getName(), approvalId);
        try{
            return success ? PtpResultUtils.ok() : PtpResultUtils.error(420, "操作失败");
        }catch (Exception e){
            e.printStackTrace();
            return PtpResultUtils.error(500);
        }
    }

}
